package com.Leetcode;

import com.structure.ListNode;

public class Index25reverseKGroup {
	public ListNode reverseKGroup(ListNode head, int k) {
		if(head == null) return head;
        ListNode a,b;
        a = b = head;
        for(int i = 0; i < k; i++) {
        	if(b == null) return head;
        	b = b.next;
        }
        ListNode node = reverseList(a,b);
        a.next = reverseKGroup(b, k);
		return node;
    }
	
	public ListNode reverseList(ListNode head,ListNode end) {
        ListNode pre = null,cur = head,nex = head;
        while(nex != end) {
        	nex = nex.next;
        	cur.next = pre;
        	pre = cur;
        	cur = nex;	
        }
        return pre;
    }
}
